package com.shenmazong.servermsgapi.controller;

import com.shenmazong.servermsgapi.service.MessageService;
import com.shenmazong.utils.ResponseResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

// (old) http://localhost:port/swagger-ui.html
// (new) http://localhost:port/doc.html

@RestController
@Slf4j
@Api(tags = "接口-即时通讯服务")
@ApiSort(value = 1)
public class MessageController {

    @Autowired
    MessageService messageService;

    // 解决example的bug
    // https://blog.csdn.net/ling1234ling1234/article/details/94635464
    @PostMapping(value = "/sendMessage")
    @ApiOperation(value = "发送消息", notes = "用户发送实时消息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="sendId", value = "发送者ID", dataType = "Long", required=true, example = "0"),
            @ApiImplicitParam(name="recvId", value = "接收者ID", dataType = "Long", required=true, example = "0"),
            @ApiImplicitParam(name="message", value = "发送的信息", dataType = "String", required=true)
    })
    public ResponseResult sendMessage(@RequestParam("sendId") Integer sendId,
                                      @RequestParam("recvId") Integer recvId,
                                      @RequestParam("message") String message) {
        // 发送了一个普通的消息
        return messageService.sendMessage(sendId, recvId, 0, message);
    }

    @PostMapping(value = "/recvMessage")
    @ApiOperation(value = "接收消息", notes = "用户接收实时消息")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="userId", value = "接收者ID", dataType = "Long", required=true, example = "0"),
            @ApiImplicitParam(name="msgId", value = "最后一条消息的ID", dataType = "Long", required=true, example = "0")
    })
    public ResponseResult recvMessage(@RequestParam("userId") Integer userId,
                                      @RequestParam("msgId") Long msgId) {
        return messageService.recvMessage(userId, msgId);
    }
}
